package com.yueya.api.bed;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.business.bed.model.Bed;
import com.yueya.business.bed.model.Floor;
import com.yueya.business.bed.model.House;
import com.yueya.business.bed.model.Room;
import com.yueya.common.constant.CodeConst;
import com.yueya.common.model.Resp;
import com.yueya.common.service.BaseService;
import net.sf.json.JSONObject;

public class IBedService extends BaseService {

	// 获取栋list
	public Resp getPageHouse(JSONObject jo) {
		pageNumber = jo.containsKey("pgn") ? jo.getInt("pgn") : pageNumber;
		pageSize = jo.containsKey("pgs") ? jo.getInt("pgs") : pageSize;
		String select = "select house_id,house_name,house_no,org_id ";
		StringBuffer sql = new StringBuffer(" from  jg_house  where 1=1 ");
		if (jo.containsKey("orgid")) {
			String orgid = jo.getString("orgid");
			sql.append(" and org_id ='" + orgid + "'");
		}
		Page<Record> page = Db.paginate(pageNumber, pageSize, select.toString(), sql.toString());
		if (page != null) {
			resp.setCode(CodeConst.SUCCESS);
			if (page.getList().size() > 0) {
				resp.setData(page);
			}
		}else {
			resp.setCode(CodeConst.WARN);
			resp.setMsg("no record");
		}
		return resp;
	}

	// 根据大楼编号获取楼层
	public Resp getPageFloor(JSONObject jo) {

		pageNumber = jo.containsKey("pgn") ? jo.getInt("pgn") : pageNumber;
		pageSize = jo.containsKey("pgs") ? jo.getInt("pgs") : pageSize;
		String select = "select floor_id,floor_name,floor_no,house_id ";
		StringBuffer sql = new StringBuffer(" from  jg_floor  where 1=1 ");
		if (jo.containsKey("houseid")) {
			String houseid = jo.getString("houseid");
			sql.append(" and house_id ='" + houseid + "'");
		}
		Page<Record> page = Db.paginate(pageNumber, pageSize, select.toString(), sql.toString());
		if (page != null) {
			resp.setCode(CodeConst.SUCCESS);
			if (page.getList().size() > 0) {
				resp.setData(page);
			}
		}else {
			resp.setCode(CodeConst.WARN);
			resp.setMsg("no record");
		}
		return resp;
	}

	// 根据楼层获取房间号
	public Resp getPageRoom(JSONObject jo) {

		pageNumber = jo.containsKey("pgn") ? jo.getInt("pgn") : pageNumber;
		pageSize = jo.containsKey("pgs") ? jo.getInt("pgs") : pageSize;

		String select = "select room_id,room_name,room_no,floor_id,room_config ";
		StringBuffer sql = new StringBuffer(" from  jg_room  where 1=1 ");

		if (jo.containsKey("floorid")) {
			String floorid = jo.getString("floorid");
			sql.append(" and floor_id ='" + floorid + "'");
		}

		Page<Record> page = Db.paginate(pageNumber, pageSize, select.toString(), sql.toString());
		if (page != null) {
			resp.setCode(CodeConst.SUCCESS);
			if (page.getList().size() > 0) {
				resp.setData(page);
			}
		}else {
			resp.setCode(CodeConst.WARN);
			resp.setMsg("no record");
		}
		return resp;
	}

	// 获取床位
	public Resp getPageBed(JSONObject jo) {

			pageNumber = jo.containsKey("pgn")?jo.getInt("pgn"):pageNumber;
			pageSize = jo.containsKey("pgs")?jo.getInt("pgs"):pageSize;
			String select = "select bed.bed_id,bed.bed_name,bed.bed_no,bed.bed_type,bed.room_id,bed.house_id,bed.floor_id,bed.status   ";
			StringBuffer sql = new StringBuffer(" from jg_bed bed ");
			sql.append(" LEFT JOIN jg_room room on room.room_id= bed.room_id");
			sql.append(" LEFT JOIN jg_floor floor on floor.floor_id= bed.floor_id");
			sql.append(" LEFT JOIN jg_house house on house.house_id= bed.house_id where 1=1");

			if (jo.containsKey("houseid")) {
				String roomid = jo.getString("houseid");
				sql.append(" and bed.house_id ='" + roomid + "'");
			}

			if (jo.containsKey("floorid")) {
				String roomid = jo.getString("floorid");
				sql.append(" and bed.floor_id ='" + roomid + "'");
			}

			if (jo.containsKey("roomid")) {
				String roomid = jo.getString("roomid");
				sql.append(" and bed.room_id ='" + roomid + "'");
			}
			Page<Record> page = Db.paginate(pageNumber, pageSize, select.toString(), sql.toString());
			if (page != null) {
				resp.setCode(CodeConst.SUCCESS);
				if (page.getList().size() > 0) {
				resp.setData(page);
				resp.setMsg("some record");
				}
			}else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("no record");
			}
		return resp;
	}
	
	//大厅展示
		@SuppressWarnings({"rawtypes", "unchecked" })
		public Resp getHallJson(JSONObject jo,Record user) {
			Map data = new HashMap<String, Object>();
			//获取楼层信息 
			List<Map<String, Object>> list =StrKit.notBlank(user.getStr("dept_id"))?
					House.dao.getAllList(user.getStr("dept_id")):
				    House.dao.getAllList();
			if(list.size()>0) {		
			    data.put("houses", list);
			    resp.setCode(CodeConst.SUCCESS);
			    resp.setData(data);
			    resp.setMsg("some record");
			}else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("no record");
			}
		    resp.setCode(CodeConst.SUCCESS);
		 
			return resp;
		}
	
	
	
	//保存楼栋
	@Before(Tx.class)
	public Resp saveHouse(JSONObject jo,Record user) {
		return jo.containsKey("houseid")?updateHouse(jo):addHouse(jo,user);
	}
	
	//保存楼层
    public Resp saveFloor(JSONObject jo) {
	   return jo.containsKey("floorid")?updateFloor(jo):addFloor(jo);
    }
    
    //保存房间
    public Resp saveRoom(JSONObject jo) {
	   return jo.containsKey("roomid")?updateRoom(jo):addRoom(jo);
    }
    
    //保存床铺
    public Resp saveBed(JSONObject jo) {
	   return jo.containsKey("bedid")?updateBed(jo):saveBed(jo);
    }

    
	//新增楼栋
	@Before(Tx.class)
	public Resp addHouse(JSONObject jo,Record user) {
		if (containsKey(jo, "housename")) {
			House house = Duang.duang(House.class);
			String housename = jo.getString("housename");
			house.set("house_name", housename);// 大楼名称
			house.set("org_id", user.getStr("org_id"));//医院orgid
			// 朝向
			if (jo.containsKey("orientation")) {
				String orientation = jo.getString("orientation");
				house.set("orientation", orientation);
			}
			
			// 楼栋编号
			if (jo.containsKey("houseno")) {
				String house_no = jo.getString("house_no");
				house.set("house_no", house_no);
			} else {
				house.set("house_no", House.no());
			}
			
			boolean bl =House.dao.save(house);    
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode("30144");
				resp.setMsg("保存失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}
	//修改楼栋
	@Before(Tx.class)
	public Resp updateHouse(JSONObject jo) {
		if (containsKey(jo, "housename")) {
			House house = Duang.duang(House.class);
			String housename = jo.getString("housename");
			house.set("house_name", housename);// 大楼名称
			// 朝向
			if (jo.containsKey("orientation")) {
				String orientation = jo.getString("orientation");
				house.set("orientation", orientation);
			}
			// 楼栋编号
			if (jo.containsKey("houseno")) {
				String house_no = jo.getString("house_no");
				house.set("house_no", house_no);
			}
			
			//所属机构
			if (jo.containsKey("orgid")) {
				String orgid = jo.getString("orgid");
				house.set("org_id", orgid);
			}
			
			boolean bl = House.dao.update(house);
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("修改成功");
			} else {
				resp.setCode("30144");
				resp.setMsg("修改失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}

	//新增楼层
	@Before(Tx.class)
	public Resp addFloor(JSONObject jo) {

		if (containsKey(jo, "floorname", "houseid")) {
			Floor floor = Duang.duang(Floor.class);
			String floorname = jo.getString("floorname");
			String houserid = jo.getString("houseid");
			
			floor.set("house_id", houserid);
			floor.set("floor_name", floorname);
			floor.set("floor_no", jo.containsKey("foorno")?jo.getString("foorno"):Floor.no());
			boolean bl = Floor.dao.save(floor);
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("新增成功");
			} else {
				resp.setCode("30144");
				resp.setMsg("新增失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
    //修改楼层
	@Before(Tx.class)
	public Resp updateFloor(JSONObject jo) {
		if (containsKey(jo, "floorname","houseid")) {
			Floor floor = Floor.dao.findById(jo.getString("floorid"));
			String floorname = jo.getString("floorname");
			String houserid = jo.getString("houseid");
			floor.set("house_id", houserid);
			floor.set("floor_name", floorname);
			if(jo.containsKey("roomno")) {
				String floorno= jo.getString("floorno");
				floor.set("floorno", floorno);
			}
			boolean bl = Floor.dao.update(floor);	
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("修改成功");
			} else {
				resp.setCode("30144");
				resp.setMsg("修改失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}
	
	//新增房间
	@Before(Tx.class)
	public Resp addRoom(JSONObject jo) {
		if (containsKey(jo, "roomname", "roomconfig", "floorid")) {
			String roomname = jo.getString("roomname");
			String roomconfig = jo.getString("roomconfig");
			String floorid = jo.getString("floorid");
			Room room = Duang.duang(Room.class);
			room.set("room_name", roomname);
			room.set("floor_id", floorid);
			room.set("room_no", Room.no());
			room.set("room_config", roomconfig);
			boolean bl =Room.dao.save(room);
			
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("新增成功");
			} else {
				resp.setCode("30144");
				resp.setMsg("新增失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
	
	//修改房间
	@Before(Tx.class)
	public Resp updateRoom(JSONObject jo) {
		if (containsKey(jo, "roomname", "roomconfig","floorid")) {
			String roomname = jo.getString("roomname");
			String roomconfig = jo.getString("roomconfig");
			String floorid = jo.getString("floorid");
			
			Room room =Room.dao.findById(jo.getString("roomid")); 
			room.set("room_name", roomname);
			room.set("room_config", roomconfig);
			room.set("floor_id", floorid);
			boolean bl =Room.dao.save(room);
			if(jo.containsKey("roomno")) {
				String roomno= jo.getString("roomno");
				room.set("roomno", roomno);
			}
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("修改成功");
			} else {
				resp.setCode("30144");
				resp.setMsg("修改失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

	//保存床铺
	@Before(Tx.class)
	public Resp addBed(JSONObject jo) {
		if (containsKey(jo, "bedname", "roomid", "bedtype")) {
			String bedname = jo.getString("bedname");
			String bedtype = jo.getString("bedtype");
			String roomid = jo.getString("roomid");

			Bed bed = Duang.duang(Bed.class);
			bed.set("bed_name", bedname);
			bed.set("bed_type", bedtype);
			bed.set("bed_no", jo.containsKey("bed_no")?jo.getString("bedno"):Bed.no());
			bed.set("room_id", roomid);

			boolean bl =Bed.dao.update(bed);
				
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode("30144");
				resp.setMsg("保存失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
	
	//修复床铺
	@Before(Tx.class)
	public Resp updateBed(JSONObject jo) {
		if (containsKey(jo, "bedname", "roomid")) {
			String bedname = jo.getString("bedname");
			String roomid = jo.getString("roomid");

			Bed bed =Bed.dao.findById(jo.getString("bedid"));
			
			if(jo.containsKey("bedno")) {
				String bedno= jo.getString("bedno");
				bed.set("bed_no", bedno);
			}
			
			bed.set("bed_name", bedname);
			bed.set("room_id", roomid);

			boolean bl = jo.containsKey("bedid")?
					Bed.dao.update(bed):
					Bed.dao.save(bed);
			
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode("30144");
				resp.setMsg("保存失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
	
	//概览数据
	public Resp getData(JSONObject jo,Record user) { 
		
		Map<String, Object> data =new HashMap<String, Object>();
		Map<String, String> param =new HashMap<String, String>();
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		param.put("org_id",user.getStr("dept_id"));
		data.put("allTotalBeds", Bed.dao.getBedStatistics(param).size());
		param.put("bed_status","0");
		data.put("allSurplusBeds", Bed.dao.getBedStatistics(param).size());
		
		List<House> list =House.dao.getList(user.getStr("dept_id"));
		Iterator<House> it =list.iterator();
		while(it.hasNext()) {
			House house = it.next();
			Map<String, Object> map =new HashMap<String, Object>();
			param.clear();
			param.put("house_id",house.getStr("house_id"));			
			map.put("totalBeds", Bed.dao.getBedStatistics(param).size());
			param.put("bed_status","0");
			map.put("surplusBeds", Bed.dao.getBedStatistics(param).size());
			result.add(map);
		}
		data.put("list", result);	
		resp.setCode(CodeConst.SUCCESS);
		resp.setMsg("success");
		resp.setData(data);
		return resp;
	}
		
	//概览楼栋的入住率
	public Resp getDataUsagerate(JSONObject jo) {
		
		if (containsKey(jo, "houseid")) {
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			Map<String, Object> map =new HashMap<String, Object>();
			Map<String, String> param =new HashMap<String, String>();
			param.put("house_id", jo.getString( "houseid"));
			param.put("bed_status","1");
			map.put("value", Bed.dao.getBedStatistics(param).size());
			map.put("name", "已用床位");
			result.add(map);
			param.clear();
			Map<String, Object> map1 =new HashMap<String, Object>();
			param.put("house_id", jo.getString( "houseid"));
			param.put("bed_status","0");
			map1.put("value",Bed.dao.getBedStatistics(param).size());
			map1.put("name", "空床位");
			result.add(map1);
			resp.setCode(CodeConst.SUCCESS);
			resp.setMsg("success");
			resp.setData(result);
		}else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		
		return resp;
	}
	
	//概览统计床位类型图表
	public Resp getDataType(JSONObject jo) {
		Map<String, Object> map =new HashMap<String, Object>(); 	
		List<Record> list = Room.dao.getRoomTypeAndNum();
		Iterator<Record> it =list.iterator();
	    
		List<String> xAxis = new ArrayList<String>();
		List<Long> yAxis = new ArrayList<Long>();
 		 while(it.hasNext()) {
			 Record record = it.next(); 
			 xAxis.add(record.getStr("roomtype"));
			 yAxis.add(record.getLong("num"));
		 }
 		map.put("xAxis", xAxis);
 		map.put("yAxis", yAxis);
 		
 		resp.setCode(CodeConst.SUCCESS);
		resp.setMsg("success");
		resp.setData(map);
	    return resp;
		}
	
	
	
	

}